#include "stdafx.h"
#include "Retriever.h"

CRetriever::CRetriever()
{

}
CRetriever::~CRetriever()
{

}

void CRetriever::CleanUp()
{
	if(m_threadMaster)
	{
		m_threadMaster = NULL;
	}
	m_arrSlaves.RemoveAll();
}

void CRetriever::AddFrame(int iFrameIdx, HBITMAP hBmp)
{
	BitmapFrame* frame = new BitmapFrame(iFrameIdx, hBmp);
	
	BOOL b = m_threadMaster->PostThreadMessage(
		CRetrieverMaster::WM_MASTERMESSAGE, 
		CRetrieverMaster::MasterWParamNewBitmapArrived, (LPARAM)frame);
	if(!b)
	{
		TRACE("Error when PostThreadMessage to master, error = %d.\r\n", GetLastError());
	}
}

void CRetriever::Start(HWND hwndNotifier, 
						CCodeBook* codeBook, 
						CFeatureExtract* extractor, 
						PatchMode patchMode /*= PatchModeNonOverlapped*/,
						int iPatchSize /*= 16*/,
						int iRegionThreshold /*= 4*/,
						char* siftKmeansModel/* = NULL*/,
						char* siftSVMModel/* = NULL*/,
						int iSlaveCnt /*= 3*/)
{
	m_threadMaster = (CRetrieverMaster*)AfxBeginThread(RUNTIME_CLASS(CRetrieverMaster),
		0, 0, CREATE_SUSPENDED, 0);
	m_threadMaster->m_bAutoDelete = TRUE;
	m_threadMaster->SetNotifier(hwndNotifier);
	m_threadMaster->ResumeThread();

	for(int i = 0; i < iSlaveCnt; ++i)
	{
		CRetriverSlave* slave = (CRetriverSlave*)AfxBeginThread(RUNTIME_CLASS(CRetriverSlave),
			0, 0, CREATE_SUSPENDED, 0);
		slave->m_bAutoDelete = TRUE;
		slave->SetMaster(m_threadMaster);
		slave->SetParams(codeBook, extractor, iPatchSize, patchMode, iRegionThreshold,
			siftKmeansModel, siftSVMModel);
		slave->ResumeThread();
		
		m_arrSlaves.Add(slave);
	}
}